$(function () {
    $("#page").load(URI + "/rbac/html/page.html");
});

//查询所有数据
function findAll() {
    //trim()去掉两边的空格
    let data = {
        status: $("#status").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    let result = myAjax("/api/dw/daily/findAll", data, "post");
    // 总条数
    if ($.isEmptyObject(result)) {
        alert("您没有权限访问!!!")

    }
    if (result.code === 200) {
        if (result.data.count != null) {
            $("#totalCount").html(result.data.count);
        }
        setData(result.data.pageInfo.list);
    } else {
        layer.alert(result.message, {icon: 2});
    }
}

//遍历查询到的数据
function setData(data) {
    let html = "";
    for (let i = 0; i < data.length; i++) {
        html += " <tr >\n" +
            "            <td>" + data[i].id + "</td>\n" +
            "            <td title='" + data[i].author + "'>" + clearNull(data[i].author) + "</td>\n" +
            "            <td title='" + data[i].time + "'>" + clearNull(data[i].time) + "</td>\n" +
            "            <td title='" + data[i].major + "'>" + clearNull(data[i].major) + "</td>\n" +
            "            <td title='" + data[i].coach + "'>" + clearNull(data[i].coach) + "</td>\n" +
            "            <td title='" + data[i].councilor + "'>" + clearNull(data[i].councilor) + "</td>\n" +
            "            <td title='" + data[i].status + "'>" + clearNull(data[i].status === '草稿' || data[i].status === '未提交' ? '<span style="color: red;">未提交</span>' : data[i].status) + "</td>\n" +
            "            <td title='" + data[i].createTime + "'>" + clearNull(data[i].createTime) + "</td>\n" +
            "            <td class=\"td-manage\">\n" +
            "                   <button class=\"layui-btn layui-btn layui-btn-xs\"  onclick=\"toEditPage('" + data[i].id + "','" + data[i].status + "')\" >" +
            "                   <i class=\"layui-icon\">&#xe642;</i>检查</button>" +
            "                   <button class=\" layui-btn layui-btn-xs\"  onclick=\"downloadDaily(" + data[i].id + ")\" >" +
            "                   <i class=\"layui-icon\">&#xe621;</i>导出文档</button>" +
            "            </td>\n" +
            "          </tr>";
    }
    $("#tbody").html(html);
}


/***
 * 清除数据中的undefined和null将其置换为空，不显示
 */

function clearNull(value) {
    return value === "null" || value === undefined ? "" : value;
}


/**
 * 检查学员日报，更改状态
 */
function toEditPage(id,status) {
    if (status === '草稿' || status === '未提交') {
        layer.alert("该学员未提交日报",{icon: 2});
        return;
    }
    if(status === '已检查'){
        layer.alert("该学员日报已检查",{icon: 1});
        return;
    }
    sessionStorage.setItem("dailyId", id);
    sessionStorage.setItem("dailyStatus", status);
    x_admin_show('检查', URI + '/rbac/html/daily/daily-check.html', 800, 600);
}

/**
 *word下载
 *
 */
function downloadDaily(id) {
    let result = myAjax("/api/dw/daily/findById", {id: id}, "post");
    let data = {
        author: result.data.author,
        time: result.data.time,
        major: result.data.major,
        coach: result.data.coach,
        councilor: result.data.councilor,
        content: result.data.content,
        event: result.data.event,
        starred: result.data.starred,
        harvest: result.data.harvest,
        doubt: result.data.doubt,
        help: result.data.help,
    };
    let info = myAjax("/api/dw/daily/download", data, "post");
    if (info.code === 200) {
        alert(info.message);
    } else {
        alert(info.message);
    }
}

